﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Presentation.Core;
using Infrastructure.System;
using Domain.Entities;
using Domain.Service.System;

namespace DDD从零起步.Controllers
{
    public class MenuController : BaseController
    {
        IMenuRepository iMenuRepository;
        public MenuController()
        {
            iMenuRepository = new MenuRepository();
        }

        //
        // GET: /Menu/

        public ActionResult Index()
        {
            var model = iMenuRepository.GetEntities();
            return View(model);
        }

        //
        // GET: /Menu/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Menu/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                WebManageMenus entity = new WebManageMenus();
                TryUpdateModel(entity);
                entity.UpdateDate = DateTime.Now;
                entity.Status = 100;
                entity.Operator = string.Empty;
                iMenuRepository.Add(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu/Edit/5

        public ActionResult Edit(int id)
        {
            return View(iMenuRepository.Find(id));
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                WebManageMenus entity = iMenuRepository.Find(id);
                TryUpdateModel(entity);
                entity.UpdateDate = DateTime.Now;
                iMenuRepository.Modify(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                iMenuRepository.Remove(new WebManageMenus { MenuID = id });
                return RedirectArtDialogToAction("操作成功", "Index");
            }
            catch (Exception ex)
            {

                return RedirectArtDialogToAction("操作失败，信息为:" + ex.Message, "Index");
            }
        }


    }
}
